﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System;
using System.IO.Compression;
using System.Net;
using System.Text;
using Noria.Web.Utility;
using File = System.IO.File;

namespace Noria.Web.Test
{
    [TestClass]
    public class UploadX
    {
        [TestMethod]
        public void DateTimeTest()
        {
            var name = "test.zip";
            name = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss.fff_") + name;
        }

        [TestMethod]
        public void UploadSim()
        {
            string file = @"D:\web\mymsdn\mm.zip";
            string url = "http://api.mymsdn.info/upload.ashx?name=mm.zip";
            //string url = "http://localhost:8076/upload.ashx?name=mm.zip";

            var req = WebRequest.CreateHttp(url);

            req.Method = WebRequestMethods.Http.Post;
            req.Headers.Add("NAuth", "Noria c3RhaW5ib3k6MTIzNDU2");

            var buffer = File.ReadAllBytes(file);

            req.GetRequestStream().Write(buffer, 0, buffer.Length);
            var res = req.GetResponse();

            var length = res.ContentLength;
            var buffer2 = new byte[length];
            res.GetResponseStream().Read(buffer2, 0, buffer2.Length);

            dynamic obj = JObject.Parse(Encoding.UTF8.GetString(buffer2));
            string name = obj.name;

            ExtractInt(name, "/");
        }

        [TestMethod]
        public void Extract()
        {
            ExtractInt("mm.zip", "/");
        }

        public void ExtractInt(string file, string path)
        {
            string url = "http://api.mymsdn.info/extract.ashx";
            //string url = "http://localhost:8076/extract.ashx";

            var req = WebRequest.CreateHttp(url);

            req.Method = WebRequestMethods.Http.Post;
            req.Headers.Add("NAuth", "Noria c3RhaW5ib3k6MTIzNDU2");

            dynamic payload = new JObject();
            payload.name = file;
            payload.path = path;
            var buffer = Encoding.UTF8.GetBytes(payload.ToString());

            req.GetRequestStream().Write(buffer, 0, buffer.Length);
            var res = req.GetResponse();
        }

        [TestMethod]
        public void Archive()
        {
            string startPath = @"D:\web\mymsdn\new";
            string zipPath = @"D:\web\mymsdn\mm.zip";
            string extractPath = @"D:\web\mymsdn\newmm";

            ZipFile.CreateFromDirectory(startPath, zipPath);
            NZip.ExtractToDirectory(zipPath, extractPath);
        }

        [TestMethod]
        public void FtpTest()
        {
            FtpClient ftp = new FtpClient("182.50.130.72", "xxxxx", "xxxxx");
            ftp.Upload("my.cc", @"D:\web\mymsdn\ftp\my.cc");
        }

    }
}
